package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.CategoryAttributeDto;
import com.yc.sole.erp.entity.CategoryAttribute;
import com.yc.sole.erp.mapper.CategoryAttributeMapper;
import com.yc.sole.erp.request.CategoryAttributeQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-13
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryAttributeRepository extends ServiceImpl<CategoryAttributeMapper, CategoryAttribute> {

    /**
     * 新增
     *
     * @param categoryAttributeDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCategoryAttribute(CategoryAttributeDto categoryAttributeDto) {
        CategoryAttribute entity = new CategoryAttribute();
        BeanUtil.copyProperties(categoryAttributeDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param categoryAttributeDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCategoryAttributeById(CategoryAttributeDto categoryAttributeDto) {
        CategoryAttribute entity = new CategoryAttribute();
        BeanUtil.copyProperties(categoryAttributeDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategoryAttributeByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<CategoryAttributeDto> selectCategoryAttributeByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), CategoryAttributeDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public CategoryAttributeDto selectCategoryAttributeById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), CategoryAttributeDto.class);
    }

    /**
     * 根据id获取
     *
     * @param name
     * @return RestResult
     */
    public CategoryAttributeDto selectCategoryAttributeByName(String name) {
        LambdaQueryWrapper<CategoryAttribute> wrapper = Wrappers.<CategoryAttribute>lambdaQuery().eq(CategoryAttribute::getName, name);
        return BeanUtil.copyProperties(this.getOne(wrapper), CategoryAttributeDto.class);
    }

    /**
     * 根据分类id查询
     *
     * @param categoryId
     * @return
     */
    public List<CategoryAttributeDto> selectByCategoryId(Long categoryId) {
        LambdaQueryWrapper<CategoryAttribute> wrapper = Wrappers.<CategoryAttribute>lambdaQuery()
                .eq(CategoryAttribute::getCategoryId, categoryId)
                .orderByAsc(CategoryAttribute::getSort);
        return BeanUtil.copyToList(this.list(wrapper), CategoryAttributeDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<CategoryAttributeDto> selectCategoryAttributePage(Paging page, CategoryAttributeQueryReq categoryAttributeQueryReq) {
        LambdaQueryWrapper<CategoryAttribute> queryWrapper = Wrappers.<CategoryAttribute>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(categoryAttributeQueryReq.getName()), CategoryAttribute::getName, categoryAttributeQueryReq.getName())
                .eq(Objects.nonNull(categoryAttributeQueryReq.getCategoryId()), CategoryAttribute::getCategoryId, categoryAttributeQueryReq.getCategoryId())
                .eq(Objects.nonNull(categoryAttributeQueryReq.getManuallyAdded()), CategoryAttribute::getManuallyAdded, categoryAttributeQueryReq.getManuallyAdded())
                .orderByAsc(CategoryAttribute::getSort);
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), CategoryAttributeDto.class);
    }
}
